package com.shukesmart.speech.utils;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

import com.shukesmart.skspeech.SKSSLHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class AudioPlayer {
    private AudioTrack audioTrack;

    public AudioPlayer(int sampleRate, boolean isStereo, int bufferSize) {
        int channelConfig = isStereo ? AudioFormat.CHANNEL_OUT_STEREO : AudioFormat.CHANNEL_IN_MONO;
        int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
        int mode = AudioTrack.MODE_STREAM;
        audioTrack = new AudioTrack(channelConfig, sampleRate, audioFormat, AudioFormat.ENCODING_PCM_16BIT, bufferSize, mode);

    }

    public void playAudio(byte[] audioData) {
        audioTrack.play();
        audioTrack.write(audioData, 0, audioData.length);
        audioTrack.flush();
    }

    public void stopPlaying() {
        if (audioTrack != null) {
            audioTrack.stop();
            audioTrack.release();
        }
    }
}